﻿namespace MemoryTest_Communications.PropertySearch.Resources
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Serialization;
    using Newtonsoft.Json;

    [JsonObject(MemberSerialization.OptIn)]
    [XmlTypeAttribute(TypeName = "response")]
    public sealed class Response
    {
        public Response()
        {
            Metadata = new List<Metadata>();
            Locations = new List<Location>();
            Listings = new List<Listing>();
        }

        [JsonProperty("application_response_code")]
        [XmlAttribute(AttributeName = "application_response_code", Type = typeof(string))]
        public string ApplicationResponseCode { get; set; }

        [JsonProperty("application_response_text")]
        [XmlAttribute(AttributeName = "application_response_text", Type = typeof(string))]
        public string ApplicationResponseText { get; set; }

        [JsonProperty("action")]
        [XmlAttribute(AttributeName = "action", Type = typeof(string))]
        public string Action { get; set; }

        [JsonProperty("attribution")]
        [XmlElement(ElementName = "attribution", Type = typeof(Attribution))]
        public Attribution Attribution { get; set; }

        [JsonProperty("created_http")]
        [XmlIgnore]
        public DateTime CreatedHttp { get; set; }

        [JsonIgnore]
        [XmlAttribute(AttributeName = "created_http", Type = typeof(string))]
        public string CreatedHttpAsString
        {
            get { return CreatedHttp.ToString(); }
            set { CreatedHttp = DateTime.Parse(value);  }
        }
         
        [JsonProperty("created_unix")]
        [XmlAttribute(AttributeName = "created_unix", Type = typeof(int))]
        public int CreatedUnix { get; set; }

        [JsonProperty("keywords")]
        [XmlAttribute(AttributeName = "keywords", Type = typeof(string))]
        public string Keywords { get; set; }

        [JsonProperty("link_to_url")]
        [XmlAttribute(AttributeName = "link_to_url", Type = typeof(string))]
        public string LinkToUrl { get; set; }

        [JsonProperty("listings")]
        [XmlIgnore]
        public IList<Listing> Listings { get; set; }

        [JsonIgnore]
        [XmlElement("listings", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Listing[] ListingsAsArray
        {
            get { return Listings.ToArray(); }
            set { Listings = new List<Listing>(value);}
        }

        [JsonProperty("encoding")]
        [XmlAttribute(AttributeName = "encoding", Type = typeof(string))]
        public string ListingType { get; set; }

        [JsonProperty("locations")]
        [XmlIgnore]
        public IList<Location> Locations { get; set; }

        [JsonIgnore]
        [XmlElement("locations", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Location[] LocationsAsArray
        {
            get { return Locations.ToArray(); }
            set { Locations = new List<Location>(value); }
        }

        [JsonProperty("page")]
        [XmlAttribute(AttributeName = "page", Type = typeof(int))]
        public int Page { get; set; }

        [JsonProperty("total_pages")]
        [XmlAttribute(AttributeName = "total_pages", Type = typeof(int))]
        public int PageCount { get; set; }

        [JsonProperty("total_results")]
        [XmlAttribute(AttributeName = "total_results", Type = typeof(string))]
        public string ResultsCount { get; set; }

        [JsonProperty("sort")]
        [XmlAttribute(AttributeName = "sort", Type = typeof(string))]
        public string Sort { get; set; }

        [JsonProperty("status_code")]
        [XmlAttribute(AttributeName = "status_code", Type = typeof(string))]
        public string StatusCode { get; set; }

        [JsonProperty("status_text")]
        [XmlAttribute(AttributeName = "status_text", Type = typeof(string))]
        public string StatusText { get; set; }

        [JsonProperty("thanks")]
        [XmlAttribute(AttributeName = "thanks", Type = typeof(string))]
        public string Thanks { get; set; }

        [JsonProperty("version")]
        [XmlAttribute(AttributeName = "version", Type = typeof(string))]
        public string Version { get; set; }

        // Metata Properties
        [JsonProperty("metadata")]
        [XmlIgnore]
        public IList<Metadata> Metadata { get; set; }

        [JsonIgnore]
        [XmlElement("metadata", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Metadata[] MetadataAsArray
        {
            get { return Metadata.ToArray(); }
            set { Metadata = new List<Metadata>(value); }
        }

        [JsonProperty("most_recent_month")]
        [XmlAttribute(AttributeName = "most_recent_month", Type = typeof(string))]
        public string MostRecentMonth { get; set; }

        [JsonProperty("most_recent_quarter")]
        [XmlAttribute(AttributeName = "most_recent_quarter", Type = typeof(string))]
        public string MostRecentQuarter { get; set; }

        public bool LocationRefinementRequired()
        {
            return Locations.Count > 1;
        }
    }
}
